package com.lifeverse.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 价值分解实体类
 * 记录价值评估的详细分解
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "value_breakdowns", indexes = {
    @Index(name = "idx_value_breakdown_assessment", columnList = "value_assessment_id"),
    @Index(name = "idx_value_breakdown_category", columnList = "category"),
    @Index(name = "idx_value_breakdown_subcategory", columnList = "subcategory")
})
public class ValueBreakdown extends BaseEntity {
    
    /**
     * 关联的价值评估
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "value_assessment_id", nullable = false)
    private ValueAssessment valueAssessment;
    
    /**
     * 分解类别
     */
    @Column(name = "category", nullable = false, length = 100)
    private String category;
    
    /**
     * 分解子类别
     */
    @Column(name = "subcategory", length = 100)
    private String subcategory;
    
    /**
     * 分解项名称
     */
    @Column(name = "item_name", nullable = false, length = 200)
    private String itemName;
    
    /**
     * 分解项描述
     */
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;
    
    /**
     * 分解项价值
     */
    @Column(name = "item_value", nullable = false, precision = 15, scale = 4)
    private BigDecimal itemValue;
    
    /**
     * 价值单位
     */
    @Column(name = "value_unit", length = 50)
    private String valueUnit;
    
    /**
     * 权重（0-1）
     */
    @Column(name = "weight", nullable = false, precision = 5, scale = 4)
    private BigDecimal weight;
    
    /**
     * 置信度（0-100）
     */
    @Column(name = "confidence_level", precision = 5, scale = 2)
    private BigDecimal confidenceLevel;
    
    /**
     * 计算方法
     */
    @Column(name = "calculation_method", length = 200)
    private String calculationMethod;
    
    /**
     * 数据来源
     */
    @Column(name = "data_source", length = 200)
    private String dataSource;
    
    /**
     * 验证状态
     */
    @Column(name = "validation_status", length = 20)
    private String validationStatus = "PENDING";
    
    /**
     * 验证结果
     */
    @Column(name = "validation_result", columnDefinition = "TEXT")
    private String validationResult;
    
    /**
     * 分解项标签（JSON格式）
     */
    @Column(name = "tags", length = 500)
    private String tags;
    
    /**
     * 分解项属性（JSON格式）
     */
    @Column(name = "attributes", columnDefinition = "TEXT")
    private String attributes;
    
    /**
     * 计算加权价值
     */
    public BigDecimal calculateWeightedValue() {
        if (itemValue == null || weight == null) {
            return BigDecimal.ZERO;
        }
        return itemValue.multiply(weight);
    }
    
    /**
     * 验证分解项
     */
    public void validate(boolean isValid, String validationComment) {
        this.validationStatus = isValid ? "VALIDATED" : "INVALID";
        this.validationResult = validationComment;
    }
    
    /**
     * 判断是否已验证
     */
    public boolean isValidated() {
        return "VALIDATED".equals(validationStatus);
    }
    
    /**
     * 判断是否无效
     */
    public boolean isInvalid() {
        return "INVALID".equals(validationStatus);
    }
    
    /**
     * 计算置信度调整后价值
     */
    public BigDecimal calculateConfidenceAdjustedValue() {
        if (itemValue == null || confidenceLevel == null) {
            return itemValue;
        }
        
        BigDecimal confidenceFactor = confidenceLevel.divide(BigDecimal.valueOf(100), 4, java.math.RoundingMode.HALF_UP);
        return itemValue.multiply(confidenceFactor);
    }
}